package com.blog.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blog.context.BaseContext;
import com.blog.dto.AddCategoryByArticleDTO;
import com.blog.dto.ArticleDTO;
import com.blog.dto.PageDTO;
import com.blog.entity.Article;
import com.blog.entity.Category;
import com.blog.entity.Works;
import com.blog.mapper.ArticleCategoryMapper;
import com.blog.mapper.ArticleMapper;
import com.blog.service.ArticleService;
import com.blog.utils.StrUtil;
import com.blog.vo.ArticleVO;
import com.blog.vo.CategoryVO;
import com.blog.vo.WorksVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private ArticleCategoryMapper articleCategoryMapper;

    @Override
    public List<ArticleVO> articleList() {
        List<ArticleVO> articleVOList = new ArrayList<>();
        Long currentId = BaseContext.getCurrentId();

        List<Article> list = articleMapper.selectByUid(currentId);
        list.forEach(article -> {
            ArticleVO articleVO = new ArticleVO();
            BeanUtils.copyProperties(article,articleVO);
            String time = StrUtil.formattedTime(article.getCreateTime());
            articleVO.setCreateTime(time);
            articleVOList.add(articleVO);
        });

        return articleVOList;
    }

    @Override
    public List<ArticleVO> articleListByValue(String value) {
        List<ArticleVO> articleVOList = new ArrayList<>();
        Long currentId = BaseContext.getCurrentId();

        List<Article> list = articleMapper.selectByUidAndValue(currentId,value);
        list.forEach(article -> {
            ArticleVO articleVO = new ArticleVO();
            BeanUtils.copyProperties(article,articleVO);
            String time = StrUtil.formattedTime(article.getCreateTime());
            articleVO.setCreateTime(time);
            articleVOList.add(articleVO);
        });
        return articleVOList;
    }

    @Override
    public ArticleVO articleListById(Long id) {
        ArticleVO articleVO = new ArticleVO();
        Article article = articleMapper.selectByUidAndId(id);
        BeanUtils.copyProperties(article,articleVO);
        articleVO.setCreateTime(StrUtil.formattedTime(article.getCreateTime()));
        return articleVO;
    }

    @Override
    public void addArticle(ArticleDTO articleDTO) {
        Long currentId = BaseContext.getCurrentId();
        Article article = new Article();
        BeanUtils.copyProperties(articleDTO,article);
        Timestamp timestamp = StrUtil.getTimestamp();
        article.setUserId(currentId);
        article.setCreateTime(timestamp);
        articleMapper.insert(article);
    }

    @Override
    public void EditArticle(ArticleDTO articleDTO) {
        Article article = new Article();
        BeanUtils.copyProperties(articleDTO,article);
        articleMapper.update(article);
    }

    @Override
    @Transactional//添加事务注解，确保同时成功
    public void deleteArticle(Long id) {
        // TODO: 需要完善

        //先删除tb_ategory_article的文章类别
//        articleCategoryMapper.deleteByArticleId(id);
        //再删除tb_article的文章
//        articleMapper.deleteById(id);

    }

    @Override
    public List<ArticleVO> pageArticle(PageDTO pageDTO) {
        List<ArticleVO> articleVOList = new ArrayList<>();
        Long currentId = BaseContext.getCurrentId();
        QueryWrapper<Article> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",currentId);
        //参数一是当前页，参数二是每页个数
        IPage<Article> articlePage = new Page<>(pageDTO.getCurrentPage(), pageDTO.getPageSize());
        List<Article> articleIPage = articleMapper.selectArticles(articlePage, queryWrapper);
        articleIPage.forEach(article -> {
            ArticleVO articleVO = new ArticleVO();
            BeanUtils.copyProperties(article,articleVO);
            articleVO.setCreateTime(StrUtil.formattedTime(article.getCreateTime()));
            articleVOList.add(articleVO);
        });
        return articleVOList;
    }

    @Override
    public Long totalArticle() {
        Long currentId = BaseContext.getCurrentId();
        return articleMapper.countArticle(currentId);
    }

    @Override
    public List<ArticleVO> articleByNoCategory() {
        List<ArticleVO> articleVOList = new ArrayList<>();
        Long currentId = BaseContext.getCurrentId();
        List<Article> list = articleMapper.articleByNoCategory(currentId);
        list.forEach(article -> {
            ArticleVO articleVO = new ArticleVO();
            BeanUtils.copyProperties(article,articleVO);
            String time = StrUtil.formattedTime(article.getCreateTime());
            articleVO.setCreateTime(time);
            articleVOList.add(articleVO);
        });
        return articleVOList;
    }

    @Override
    public List<ArticleVO> articleByCategory(Long categoryId) {
        List<ArticleVO> articleVOList = new ArrayList<>();
        Long currentId = BaseContext.getCurrentId();
        List<Article> list = articleMapper.articleByCategory(categoryId, currentId);
        list.forEach(article -> {
            ArticleVO articleVO = new ArticleVO();
            BeanUtils.copyProperties(article, articleVO);
            articleVO.setCreateTime(StrUtil.formattedTime(article.getCreateTime()));
            articleVOList.add(articleVO);
        });
        return articleVOList;
    }

    @Override
    public List<CategoryVO> getOneArticleByCategory(Long articleId) {
        Long currentId = BaseContext.getCurrentId();
        List<CategoryVO> categoryVOList = new ArrayList<>();
        List<Category> categoryList = articleMapper.getCategory(articleId, currentId);
        categoryList.forEach(category -> {
            CategoryVO categoryVO = new CategoryVO();
            BeanUtils.copyProperties(category, categoryVO);
            categoryVOList.add(categoryVO);
        });
        return categoryVOList;
    }

    @Override
    public void AddCategoryByArticle(AddCategoryByArticleDTO addCategoryByArticleDTO) {
        //先去 tb_category_article表 删除这个文章的所有类别
        articleCategoryMapper.deleteByArticleId(addCategoryByArticleDTO.getCurrentArticleId());
        //再去添加这个作品要添加的所有类别
        articleCategoryMapper.insertBatch(addCategoryByArticleDTO.getCategoryIds(), addCategoryByArticleDTO.getCurrentArticleId());
    }

    @Override
    public List<ArticleVO> recommendArticleList(Long uid) {
        List<ArticleVO> articleVOS = new ArrayList<>();
        List<Article> list = articleMapper.recommendSelect(uid,1L);
        list.forEach(article -> {
            ArticleVO articleVO = new ArticleVO();
            BeanUtils.copyProperties(article,articleVO);
            articleVO.setCreateTime(StrUtil.formattedTime(article.getCreateTime()));
            articleVOS.add(articleVO);
        });
        return articleVOS;
    }

    @Override
    public List<ArticleVO> timeNewArticleList(Long uid) {
        List<ArticleVO> articleVOS = new ArrayList<>();
        List<Article> list = articleMapper.timeNewSelect(uid);
        list.forEach(article -> {
            ArticleVO articleVO = new ArticleVO();
            BeanUtils.copyProperties(article,articleVO);
            articleVO.setCreateTime(StrUtil.formattedTime(article.getCreateTime()));
            articleVOS.add(articleVO);
        });
        return articleVOS;
    }

    //用户端：分页查询文章列表
    @Override
    public List<ArticleVO> userPageArticleList(PageDTO pageDTO) {
        List<ArticleVO> articleVOList = new ArrayList<>();
        QueryWrapper<Article> queryWrapper = new QueryWrapper<>();
        //传入的用户ID
        queryWrapper.eq("user_id",pageDTO.getUid());
        //参数一是当前页，参数二是每页个数
        IPage<Article> articlePage = new Page<>(pageDTO.getCurrentPage(), pageDTO.getPageSize());
        List<Article> articleIPage = articleMapper.selectArticles(articlePage, queryWrapper);
        articleIPage.forEach(article -> {
            ArticleVO articleVO = new ArticleVO();
            BeanUtils.copyProperties(article,articleVO);
            articleVO.setCreateTime(StrUtil.formattedTime(article.getCreateTime()));
            articleVOList.add(articleVO);
        });
        return articleVOList;
    }

    @Override
    public Long userTotalArticle(Long uid) {
        return articleMapper.countArticle(uid);
    }

    @Override
    public ArticleVO oneArticleById(Long aid) {
        Article article = articleMapper.selectByUidAndId(aid);
        ArticleVO articleVO = new ArticleVO();
        BeanUtils.copyProperties(article,articleVO);
        articleVO.setCreateTime(StrUtil.formattedTime(article.getCreateTime()));
        return articleVO;
    }

    @Override
    public List<CategoryVO> getCategoryByArticleID(Long aid) {
        return articleMapper.getCategoryByArticle(aid);
    }

    @Override
    public List<ArticleVO> IsRecommendArticle(Long IsRecommend) {
        List<ArticleVO> articleVOList = new ArrayList<>();
        Long currentId = BaseContext.getCurrentId();
        List<Article> articles = articleMapper.recommendSelect(currentId, IsRecommend);
        articles.forEach(article -> {
            ArticleVO articleVO = new ArticleVO();
            BeanUtils.copyProperties(article,articleVO);
            articleVO.setCreateTime(StrUtil.formattedTime(article.getCreateTime()));
            articleVOList.add(articleVO);
        });
        return articleVOList;
    }

    @Override
    public void EditArticleRecommend(Long articleId, Long isRecommend) {
        Article article = new Article();
        article.setId(articleId);
        article.setIsRecommend(isRecommend);
        articleMapper.update(article);
    }
}
